package com.zego.wawaji.base.observer;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.noble.library.basic.sp.PSP;
import com.noble.library.basic.Constant;
import com.zego.wawaji.base.invoker.Invoker;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.base.utils.IntervalUtil;
import com.zego.wawaji.base.utils.NotifyUtil;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

import java.util.LinkedList;

/**
 * Application中activity生命周期监测回调，>=API14
 * Created by ZRP on 2016/9/8.
 */
public class PActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    // 本地模拟的activity栈链表，记录activity跳转行为
    private LinkedList<String> activities = new LinkedList<>();
    private volatile boolean isForeground = false;//最后的Activity是否属于前台显示

    /**
     * @return 获取本地模拟的activity栈链表
     */
    public LinkedList<String> getActivities() {
        return activities;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        AppTinkerLike.activity = activity;
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        AppTinkerLike.activity = activity;
        // 本地模拟一个只存储10条记录的activity栈
        activities.add(activity.getClass().getSimpleName());
        if (activities.size() > 10) activities.remove(0);

        if (!PSP.getInstance().getBoolean(Constant.APP_IS_FOREGROUND, false)) {  // 从后台进入app，检查更新
            if (ModuleMgr.getLoginMgr().isExitDouble()) {  // 上次退出是双击退出的，不做重登录
                ModuleMgr.getWawaMgr().reqNewVersion(false, null);
                ModuleMgr.getLoginMgr().updateExitDouble(false);
            } else if (IntervalUtil.needReLogin()) {  // 需要重新登录
                PToast.showShort("登录失效，请重新登录");
                ModuleMgr.getLoginMgr().exitLogin();
            } else {
                ModuleMgr.getWawaMgr().reqNewVersion(false, false, null);
            }
        }

        if (!isForeground) {
            isForeground = true;
            ModuleMgr.getPushMgr().resetJPushTag(true);
            PSP.getInstance().put(Constant.APP_IS_FOREGROUND, isForeground);
            NotifyUtil.getInstance().clear(); // 由后台切换到前台，清理通知栏
            Invoker.getInstance().doInJS(Invoker.JSCMD_refresh_web, null);  // 后台过来通知刷新
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        if (AppTinkerLike.activity == activity) {
            isForeground = false;
            ModuleMgr.getPushMgr().resetJPushTag(false);
            PSP.getInstance().put(Constant.APP_IS_FOREGROUND, isForeground);
            IntervalUtil.updateReLoginDate();
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}
